package com.spacing.app;

import java.util.ArrayList;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockListActivity;
import com.senseilearning.spanish.R;
import com.spacing.app.WordsViewActivity.RowModel;
import com.spacing.app.WordsViewActivity.ViewWrapper;
import com.spacing.app.WordsViewActivity.WordlistAdapter;
import com.spacing.app.data.Chapters;

import android.app.ListActivity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

public class ChapterViewActivity extends SherlockListActivity {
	Chapters chapters;
	int chapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Intent intent = getIntent();
		//requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		
		setContentView(R.layout.chapters_list);
		Intent intent = getIntent();
		chapters = (Chapters) intent.getSerializableExtra("chapters");
		chapter = intent.getExtras().getInt("chapter");
		
		ActionBar ab = getSupportActionBar();
		ab.setTitle(chapters.get(chapter).getName());
		
		ArrayList<String> chapterNames = chapters.get(chapter).getNames();
		ArrayList<RowModel> list = new ArrayList<RowModel>();

		

		SchemaHelper sh = new SchemaHelper(this);
		
		//for (int i = 0; i < chapter_names.size(); i++) {
		for (int i = 0; i < chapterNames.size(); i++) {
			/*String word ="";
			for(int j=0; j<5;j++){
				if(sh.getSectionSize(chapter, i)>j){
					word += sh.getWordlist2(chapter, i)[j]+", ";
					//word += chapters.get(chapter).get(i).get(j).getWord2()+", ";
				}
			}
			word+=" ...";*/
			//list.add(new RowModel(sh.getSectionNames(chapter)[i], word));
			list.add(new RowModel(chapterNames.get(i), sh.getDatepracticed(chapterNames.get(i))));
		}
		setListAdapter(new WordlistAdapter(list));
	}
	
	@Override
	public void onListItemClick(ListView parent, View v, int position, long id) {
		Intent intent = new Intent(this, SectionViewActivity.class);
		intent.putExtra("chapters", chapters);
		intent.putExtra("chapter", chapter);
		intent.putExtra("section", position);
		startActivity(intent);
	}

	private RowModel getModel(int position) {
		return (((WordlistAdapter) getListAdapter()).getItem(position));
	}

	class WordlistAdapter extends ArrayAdapter<RowModel> {
		WordlistAdapter(ArrayList<RowModel> list) {
			super(ChapterViewActivity.this, R.layout.section, list);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			ViewWrapper wrapper;
			if (row == null) {
				LayoutInflater inflater = getLayoutInflater();

				row = inflater.inflate(R.layout.section, parent, false);
				wrapper = new ViewWrapper(row);
				row.setTag(wrapper);

			} else {
				wrapper = (ViewWrapper) row.getTag();
			}

			RowModel model = getModel(position);

			wrapper.getLabel().setText(model.getSectionName());
			wrapper.getLabel2().setText(model.getDate());
			
			int s = position+1;
			int c = chapter+1;
			
			
			String uri = "drawable/c"+c+"s"+s; // afbeelding selecteren aan de hand van bestandsnaam
		    try{
			
			int imageResource = getResources().getIdentifier(uri, null, getPackageName());
		    ImageView imageView = wrapper.getImage();
		    Drawable image = getResources().getDrawable(imageResource);
		    imageView.setImageDrawable(image);
		    }
		    catch(Exception ex){
		    	uri = "drawable/not_found"; // afbeelding selecteren aan de hand van bestandsnaam
		    	int imageResource = getResources().getIdentifier(uri, null, getPackageName());
		    	ImageView imageView = wrapper.getImage();
			    Drawable image = getResources().getDrawable(imageResource);
			    imageView.setImageDrawable(image);
		    }

			return (row);
		}
	}
	
	class ViewWrapper {
		View base;
		TextView label = null;
		TextView label2 = null;
		ImageView image = null;
		
		
		ViewWrapper(View base) {
			this.base = base;
		}

		TextView getLabel() {
			if (label == null) {
				label = (TextView) base.findViewById(R.id.section_name);
			}
			return (label);
		}
		TextView getLabel2() {
			if (label2 == null) {
				label2 = (TextView) base.findViewById(R.id.textViewDate);
			}
			return (label2);
		}
		
		ImageView getImage() {
			if (image == null) {
				image = (ImageView) base.findViewById(R.id.myImageView);
			}
			return image;
		}
		
		
	}

	class RowModel {
		String section_name;
		String date;

		RowModel(String section_name, String date) {
			this.section_name = section_name;
			this.date = date;
		}

		public String getSectionName() {
			return section_name;
		}

		public String getDate() {
			return date;
		}
	}

}